package com.hkyp.ss.course.web.controller;

import com.hkyp.ss.course.apis.CommentService;
import com.hkyp.ss.course.apis.dto.common.PageReq;
import com.hkyp.ss.course.apis.dto.common.PageResp;
import com.hkyp.ss.course.apis.dto.request.CommentDTO;
import com.hkyp.ss.course.apis.dto.request.CommentLikeDTO;
import com.hkyp.ss.course.apis.dto.response.FirstCommentVO;
import com.hkyp.ss.course.apis.utils.BeanMapper;
import com.hkyp.ss.course.web.dto.base.ResultBean;
import com.hkyp.ss.course.web.dto.request.WebCommentLikeDTO;
import com.hkyp.ss.course.web.dto.request.WebCommentDTO;
import com.hkyp.ss.course.web.utils.UserSessionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(description = "评论")
@Slf4j
@RestController
@RequestMapping(value = "/a/h/")
public class CommentController {


    @Reference(version = "1.0")
    private CommentService commentService;


    /**
     * 课评的评论,一级评论
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "评论列表")
    @RequestMapping(value = "/comment/list/{ccId}", method = RequestMethod.GET)
    public ResultBean<PageResp<FirstCommentVO>> getCommentToCourComm(@PathVariable Long ccId, PageReq pageReq){

        return new ResultBean<>( commentService.selectCommentToCourCommList(ccId,pageReq,UserSessionUtils.getUserIdCanBeNull()));

    }



    /**
     * 课评的评论,一级评论
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "发表评论")
    @RequestMapping(value = "/api/comment", method = RequestMethod.POST)
    public ResultBean<PageResp<FirstCommentVO>> addCommentToCourComm(@Valid @RequestBody WebCommentDTO webCommentDTO){


        long userId = UserSessionUtils.getUserId();

        commentService.addComment(userId,BeanMapper.map(webCommentDTO,CommentDTO.class));
        return new ResultBean<>();

    }


    /**
     * 评论的点赞
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "评论或回复的点赞")
    @RequestMapping(value = "/api/comment/{cid}/like", method = RequestMethod.POST)
    public ResultBean<PageResp<FirstCommentVO>> addCommentToCourComm(@PathVariable Long cid, @Valid @RequestBody WebCommentLikeDTO commentLikeDTO){

        Long userId = UserSessionUtils.getUserId();
        CommentLikeDTO likeDTO = BeanMapper.map(commentLikeDTO, CommentLikeDTO.class);
        likeDTO.setCid(cid);
        commentService.addCommentOrReplyLike(userId,likeDTO);
        return new ResultBean<>();

    }



}
